home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 4 / Meeting Pearls Vol. IV (1996)(GTI - Schatztruhe)[!].iso / Pearls / libs / bgui / Demos / StringHook.c < prev    next >
C/C++ Source or Header  |  1995-05-27  |  7KB  |  175 lines

  1. ;/* Execute me to compile with DICE V3.0
  2. dcc stringhook.c -proto -mi -ms -mRR -lbgui
  3. quit
  4. */
  5. /*
  6.  *      STRINGHOOK.C
  7.  *
  8.  *      (C) Copyright 1995 Jaba Development.
  9.  *      (C) Copyright 1995 Jan van den Baard.
  10.  *          All Rights Reserved.
  11.  */
  12.  
  13. #include "democode.h"
  14.  
  15. #include <ctype.h>
  16.  
  17. /*
  18. **      Object ID's.
  19. **/
  20. #define ID_QUIT                 1
  21.  
  22. /*
  23. **      Info text.
  24. **/
  25. UBYTE  *WinInfo = ISEQ_C "This demo shows you how to include a\n"
  26.                   "string object edit hook. The string object\n"
  27.                   "has a edit hook installed which only allows\n"
  28.                   "you to enter hexadecimal characters. It will\n"
  29.                   "also convert the character you click on to 0.";
  30.  
  31. /*
  32. **      String object edit hook. Copied from the
  33. **      RKM-Manual Libraries. Page 162-166.
  34. **/
  35. SAVEDS ASM HexHookFunc( REG(a0) struct Hook *hook, REG(a2) struct SGWork *sgw, REG(a1) ULONG *msg )
  36. {
  37.         ULONG           rc = ~0;
  38.  
  39.         switch ( *msg ) {
  40.  
  41.                 case    SGH_KEY:
  42.                         /*
  43.                         **      Only allow for hexadecimal characters and convert
  44.                         **      lowercase to uppercase.
  45.                         **/
  46.                         if ( sgw->EditOp == EO_REPLACECHAR || sgw->EditOp == EO_INSERTCHAR ) {
  47.                                 if ( ! isxdigit( sgw->Code )) {
  48.                                         sgw->Actions |= SGA_BEEP;
  49.                                         sgw->Actions &= ~SGA_USE;
  50.                                 } else {
  51.                                         sgw->WorkBuffer[ sgw->BufferPos - 1 ] = toupper( sgw->Code );
  52.                                 }
  53.                         }
  54.                         break;
  55.  
  56.                 case    SGH_CLICK:
  57.                         /*
  58.                         **      Convert the character under the
  59.                         **      cursor to 0.
  60.                         **/
  61.                         if ( sgw->BufferPos < sgw->NumChars )
  62.                                 sgw->WorkBuffer[ sgw->BufferPos ] = '0';
  63.                         break;
  64.  
  65.                 default:
  66.                         rc = 0L;
  67.                         break;
  68.         }
  69.         return( rc );
  70. }
  71.  
  72. /*
  73. **      Uncomment the below typedef if your compiler
  74. **      complains about it.
  75. **/
  76.  
  77. /* typedef ULONG (*HOOKFUNC)(); */
  78.  
  79. struct Hook HexHook = { NULL, NULL, (HOOKFUNC)HexHookFunc, NULL, NULL };
  80.  
  81. VOID StartDemo( void )
  82. {
  83.         struct Window           *window;
  84.         Object                  *WO_Window, *GO_Quit;
  85.         ULONG                    signal, rc;
  86.         BOOL                     running = TRUE;
  87.  
  88.         /*
  89.         **      Create the window object.
  90.         **/
  91.         WO_Window = WindowObject,
  92.                 WINDOW_Title,           "String Edit Hook Demo",
  93.                 WINDOW_AutoAspect,      TRUE,
  94.                 WINDOW_LockHeight,      TRUE,
  95.                 WINDOW_RMBTrap,         TRUE,
  96.                 WINDOW_MasterGroup,
  97.                         VGroupObject, HOffset( 4 ), VOffset( 4 ), Spacing( 4 ), GROUP_BackFill, SHINE_RASTER,
  98.                                 StartMember,
  99.                                         InfoFixed( NULL, WinInfo, NULL, 5 ),
  100.                                 EndMember,
  101.                                 StartMember,
  102.                                         HGroupObject, HOffset( 4 ), VOffset( 4 ), FRM_Type, FRTYPE_BUTTON, FRM_Recessed, TRUE,
  103.                                                 StartMember,
  104.                                                         StringObject,
  105.                                                                 LAB_Label,              "Only HEX characters:",
  106.                                                                 LAB_Style,              FSF_BOLD,
  107.                                                                 RidgeFrame,
  108.                                                                 STRINGA_MaxChars,       256,
  109.                                                                 STRINGA_EditHook,       &HexHook,
  110.                                                         EndObject,
  111.                                                 EndMember,
  112.                                         EndObject,
  113.                                 EndMember,
  114.                                 StartMember,
  115.                                         HGroupObject,
  116.                                                 VarSpace( 50 ),
  117.                                                 StartMember, GO_Quit  = KeyButton( "_Quit",  ID_QUIT  ), EndMember,
  118.                                                 VarSpace( 50 ),
  119.                                         EndObject, FixMinHeight,
  120.                                 EndMember,
  121.                         EndObject,
  122.         EndObject;
  123.  
  124.         /*
  125.         **      Object created OK?
  126.         **/
  127.         if ( WO_Window ) {
  128.                 /*
  129.                 **      Assign the key to the button.
  130.                 **/
  131.                 if ( GadgetKey( WO_Window, GO_Quit,  "q" )) {
  132.                         /*
  133.                         **      try to open the window.
  134.                         **/
  135.                         if ( window = WindowOpen( WO_Window )) {
  136.                                 /*
  137.                                 **      Obtain it's wait mask.
  138.                                 **/
  139.                                 GetAttr( WINDOW_SigMask, WO_Window, &signal );
  140.                                 /*
  141.                                 **      Event loop...
  142.                                 **/
  143.                                 do {
  144.                                         Wait( signal );
  145.                                         /*
  146.                                         **      Handle events.
  147.                                         **/
  148.                                         while (( rc = HandleEvent( WO_Window )) != WMHI_NOMORE ) {
  149.                                                 /*
  150.                                                 **      Evaluate return code.
  151.                                                 **/
  152.                                                 switch ( rc ) {
  153.  
  154.                                                         case    WMHI_CLOSEWINDOW:
  155.                                                         case    ID_QUIT:
  156.                                                                 running = FALSE;
  157.                                                                 break;
  158.                                                 }
  159.                                         }
  160.                                 } while ( running );
  161.                         } else
  162.                                 Tell( "Could not open the window\n" );
  163.                 } else
  164.                         Tell( "Could not assign gadget keys\n" );
  165.                 /*
  166.                 **      Disposing of the window object will
  167.                 **      also close the window if it is
  168.                 **      already opened and it will dispose of
  169.                 **      all objects attached to it.
  170.                 **/
  171.                 DisposeObject( WO_Window );
  172.         } else
  173.                 Tell( "Could not create the window object\n" );
  174. }
  175.